// pages/qufahuo/qufahuo.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeIndex:0,
    wuliuInfo:'',//选择的物流信息
    id:'',
    suminfo:'',
    kehuorchangjia:true,//默认用户，fas中转仓
    wuliudanhao:''
  },
  // 获取输入的物流单号
  getwuliudanhao(e){
    // console.log(e.detail.value)
    
    let wuliudanhao = e.detail.value.replace(/[^a-zA-Z0-9]/g,'')
    this.setData({
      wuliudanhao,
    })
  },
  blurdanhao(e){
    // /[^a-zA-Z0-9]/g
    let wuliudanhao = e.detail.value.replace(/[^a-zA-Z0-9]/g,'');
    this.setData({
      wuliudanhao
    })
  },

  // tofahuo(){
  //   let that = this
  //   console.log(that.data.id)
  //   wx.request({
  //     url: app.globalData.myurl,
  //     data: {
  //       cmd: "getOrderInfo",
  //       shopId: wx.getStorageSync('shopId'),
  //       userId: wx.getStorageSync('userId'),
  //       id: that.data.id
  //     },
  //     header: {
  //       "content-type": "application/x-www-form-urlencoded",
  //       "token": wx.getStorageSync('token')
  //     },
  //     success(res) {
  //       console.log(res.data,'发货详情')
  //       if (res.data.zjstate == 1) {
  //         setTimeout(function () {
  //           wx.hideLoading()
  //           wx.stopPullDownRefresh() //停止下拉刷新
  //         }, 0)
  //         setTimeout(function () {
  //           wx.showModal({
  //             title: '您的账号已在其他设备登录，请重新登录！',
  //             showCancel: false, //是否显示取消按钮false去掉取消按钮
  //             success: function (res) {
  //               if (res.confirm) {
  //                 wx.clearStorageSync()
  //                 wx.reLaunch({
  //                   url: '/pages/login/login',
  //                 })
  //               } else {
  //                 wx.clearStorageSync()
  //                 wx.reLaunch({
  //                   url: '/pages/login/login',
  //                 })
  //               }
  //             }
  //           })
  //         }, 0)
  //         return
  //       }
  //       console.log(res)
        
  //       if (res.data.result == 1) {
  //         setTimeout(function () {
  //           wx.hideLoading()
  //         }, 0)
  //         setTimeout(function () {
  //           wx.showToast({
  //             title: res.data.resultNote,
  //             icon: 'none',
  //             duration: 1500
  //           })
  //         }, 0)
          
  //       } else {
  //         let obj = res.data.orderBean
  //         console.log(obj)
  //         for (var j in obj.goodsList) {
  //           console.log(obj.goodsList[j])
  //           obj.goodsTypes = obj.goodsList[j].goodsType
  //           if (obj.goodsList[j].goodsType == 4 || obj.goodsList[j].goodsType == 5) {
  //             if (obj.goodsList[j].actdesc && obj.goodsList[j].freeprice) {
  //               obj.actdesc = obj.goodsList[j].actdesc
  //               obj.freeprice = obj.goodsList[j].freeprice
  //               obj.goodsType = obj.goodsList[j].goodsType
  //             }
  //           }
  //         }
  //         that.setData({
  //           suminfo: obj
  //         })
  //        console.log(that.data.suminfo)
  //         setTimeout(function () {
  //           wx.hideLoading()
  //         }, 0)
  //       }
  //       that.fahuo()
  //     }
  //   });
  // },
  
  // 确认发货
  fahuo(){
    let that = this;

    let wuliuInfo = that.data.wuliuInfo
    let suminfo = that.data.suminfo
    let dstate, dname, dno, dtype, qistid, qname, qphone, qaddr, qtownid
    if (!wuliuInfo){
      wx.showToast({
        title: '请先选择物流',
        icon: 'none'
      })
      return
    }
    dno = that.data.wuliudanhao
    if (!dno) {
      wx.showToast({
        title: '请输入物流单号',
        icon: 'none'
      })
      return
    }
    if (wuliuInfo.code==0) {
      dstate = 0
    } else {
      dstate = 1
    }
    dname = wuliuInfo.name
    qistid = ''
    qname = ''
    qphone = ''
    qaddr = ''
    qtownid = ''
    dtype = 0
    
    if (!that.data.kehuorchangjia){
      if (!suminfo.distid){
        wx.showToast({
          title: '该区域暂时无中转仓',
          icon: 'none'
        })
        return
      }else{
        dtype = 1
        qistid = suminfo.distid
        qname = suminfo.dname
        qphone = suminfo.dphone 
        qaddr = suminfo.daddr
        qtownid = suminfo.dtownid
      }
    }
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "upDdelivery",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        orderList: JSON.stringify([that.data.id]),
        dstate: dstate,
        dname: dname,
        dno: dno,
        dtype: dtype,
        fstate:0,
        qistid: qistid,
        qname: qname,
        qphone: qphone,
        qaddr: qaddr,
        qtownid: qtownid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        setTimeout(function () {
          wx.hideLoading()
        }, 0)
        setTimeout(function () {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1500
          })
        }, 0)
        if (res.data.result == 1 || res.data.result == -1) {
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)

        }
      }
    });
  },
  // 客户地址或者中转仓地址
  setchangedizhi(){
    let that = this
    that.setData({
      kehuorchangjia: !that.data.kehuorchangjia
    })
  },
  // 获取该订单信息
  getdingdaninfo(){
    let that = this
    console.log(that.data.id)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getOrderInfo",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        id: that.data.id
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        console.log(res.data,'发货详情')
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let obj = res.data.orderBean
          console.log(obj)
          for (var j in obj.goodsList) {
            console.log(obj.goodsList[j])
            obj.goodsTypes = obj.goodsList[j].goodsType
            if (obj.goodsList[j].goodsType == 4 || obj.goodsList[j].goodsType == 5) {
              if (obj.goodsList[j].actdesc && obj.goodsList[j].freeprice) {
                obj.actdesc = obj.goodsList[j].actdesc
                obj.freeprice = obj.goodsList[j].freeprice
                obj.goodsType = obj.goodsList[j].goodsType
              }
            }
          }
          that.setData({
            suminfo: obj
          })
         console.log(that.data.suminfo)
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  chooseWuliu(){
    wx.navigateTo({
      url: '/pages/order/page/xuanzeWuliu/xuanzeWuliu',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getdingdaninfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})